include layout/header.jade
div.page-content
    div#wide.modal.fade(role="basic")
        div.modal-dialog.modal-wide
            div.modal-content
                div.modal-header
                    button.close(type="button",data-dismiss="modal")
                    h4 添加视频
                div.modal-body.row
                    div.col-md-10.col-md-offset-1
                        form#videoform.form-horizontal(role="form")
                            div.form-group
                                label.control-label.col-md-2 标签
                                div.col-md-8
                                    input#title.form-control(placeholder='章节名称',name='name')
                            div.form-group
                                label.control-label.col-md-2 标签
                                div.col-md-8
                                    input#videosrc.form-control(placeholder='路径',name='path')
                                a#videoupload.btn.green 上传
                            div#videoprogress.form-group
                                div.col-md-8.col-md-offset-2
                                    div.progress
                                        div.progress-bar.progress-bar-success(style="width:0%")
                div.modal-footer
                    div.col-md-10.col-md-offset-1
                        div.col-md-8.col-md-offset-2
                            button.btn.green(type="button",data-dismiss="modal") 取消
                            button#save.btn.green(type="submit") 保存
    div.modal-header
        h4.modal-title 添加课程
        input#uploadvideo("type"="file",name="file",style="opacity:0;float:right")
        input#uploadimage("type"="file",name="file",style="opacity:0;float:right")
        input#uploadfile("type"="file",name="file",style="opacity:0;float:right")
    form.form-horizontal(role="form",action="save",method="post")
        div.modal-body.row
            div.col-md-10.col-md-offset-1
                div.form-group
                    label.control-label.col-md-2 课程名
                    div.col-md-8
                        input(type='hidden',name='id',value='#{lession._id}')
                        input.form-control(placeholder='标签',name='name',value='#{lession.lessionname}')
                div.form-group
                    label.control-label.col-md-2 课程
                    div.col-md-8
                        input#e2.form-control.select2.select2-offscreen(type="hidden",placeholder="选择课程",data-val="#{lession.typename}",name="typeid",value="#{lession.typeid}")
                div.form-group
                    label.control-label.col-md-2 图片
                    div.col-md-8
                        if lession.img==""
                            image#image.img-thumbnail(src="http://news.baidu.com/resource/img/logo_news.gif")
                        else
                            image#image.img-thumbnail(src='#{lession.img}')
                        input#imagesrc(type="hidden",placeholder='标签',name='img' value='#{lession.img}')
                    a#imageupload.btn.green 上传
                div.form-group.videoform
                    label.control-label.col-md-2 课程
                    div.col-md-8
                        div.portlet.box.blue
                            div.portlet-title
                                div.caption 课程
                                div.actions
                                    a.btn.btn-success(data-toggle="modal",href="#wide") 添加视频
                            div#content.portlet-body
                                each val in lession.res
                                    div.alert.alert-warning
                                        div.row
                                            div.col-md-6
                                                h2 #{val.name}
                                            div.col-md-6
                                                button.btn.red.delete(type="button",style="margin-top: 20px;") 删除
                                            input(type="hidden",name="names[]",value="#{val.name}")
                                            input(type="hidden",name="paths[]",value="#{val.path}")
                div.form-group
                    label.control-label.col-md-2 标签
                    div.col-md-8
                        textarea.form-control(placeholder='标签',name='desc',value='#{lession.desc}') #{lession.desc}
        div.modal-footer
            div.col-md-10.col-md-offset-1
                div.col-md-8.col-md-offset-2
                    a.btn.green(href="./") 取消
                    button.btn.green(type="submit") 保存
script(type='text/javascript').
      aoColumnDefs = [];
include layout/footer.jade
script(src="http://tinymce.cachefly.net/4.1/tinymce.min.js")
script(type='text/javascript').
    $(document).ready(function(){
        $("#e2").select2({
            minimumInputLength: 1,
            initSelection : function (element, callback) {
                if(element.val()){
                    var data = {text: element.attr("data-val"), id: element.val()};
                    callback(data);
                }else{
                    callback({});
                }
            },
            query: function (query) {
                $.get("/admin/type/search", {name:query.term}, // 提交的页面
                function(data1) {
                    var data = {results: []}, i, j, s;
                    for (i = 0; i < data1.length; i++) {
                        data.results.push({id: data1[i]._id, text: data1[i].name});
                    }
                    query.callback(data);
                });
            }
        }); 

        tinymce.init({selector:'textarea'});

        $("#save").click(function(){
            $("#content").append($('<div class="alert alert-warning"><div class="row"><div class="col-md-6"><h2>'+$('#title').val()+'</h2></div><div class="col-md-6"><button class="btn red delete" style="margin-top: 20px;">删除</button></div><input type="hidden" name="names[]" value='+$('#title').val()+'><input type="hidden" name="paths[]" value='+'/' +$('#videosrc').val()+'></div></div>'));
            $("#wide").modal('hide');
            $("#videoform")[0].reset();
            $("#image").attr("src","http://news.baidu.com/resource/img/logo_news.gif");
        });

        $("#video").click(function(){
                $(".videoform").show();
                $(".scormform").hide();
                $(".videoform").removeClass("hidden");

            })
        $("#scorm").click(function(){
                $(".videoform").hide();
                $(".scormform").show();
                $(".scormform").removeClass("hidden");
            })

        var currentBtn = null;
        $("#content").on('click','.delete',function(){
                $(this).parent().parent().parent().remove();
                
            });
        $("#content").on('click','.insertText',function(){
            var textarea = $("<div></div>").append("<textarea name='param[]' class='form-control'></textarea><p><input type='hidden' name='type[]' value='text'><a class='btn blue insertText'>插入文字</a><a class='btn blue insertImg'>插入图片</a><a class='btn red delete'>删除</a>")
                textarea.insertAfter($(this).parent().parent());
            });
        $("#content").on('click','.insertImg',function(){
                currentBtn = $(this).parent().parent();
                $("#addImage").click();
            });
        $("#upload").click(function(){
            $("#uploadfile").click();
        });
        $("#videoupload").click(function(){
            $("#uploadvideo").click();
        });
        $("#imageupload").click(function(){
            $("#uploadimage").click();
        });
       $("#uploadfile").fileupload({
           url:"/file-upload",
           autoUpload:true,
           done:function(e,data){
               $("#upload").prev().find("input").val(data.result.path);
           },
           progressall:function(e,data){
                var progress = parseInt(data.loaded/data.total*100,10);
                $("#progress .progress-bar").css('width',progress+'%');
           }
        });
       $("#uploadimage").fileupload({
           url:"/file-upload",
           autoUpload:true,
           done:function(e,data){
               $("#imageupload").prev().find("input").val("/"+data.result.path);
               $("#image").attr("src","/"+data.result.path);
           }
        });
       $("#uploadvideo").fileupload({
           url:"/file-upload",
           autoUpload:true,
           done:function(e,data){
               $("#videoupload").prev().find("input").val(data.result.path);
           },
           progressall:function(e,data){
                var progress = parseInt(data.loaded/data.total*100,10);
                $("#videoprogress .progress-bar").css('width',progress+'%');
           }
        });
    });